<?php

namespace app\common\mysql;

use think\Db;

class LetterUsers extends Db {

    public static $tableName = 'clt_letter_users';
    protected $_tableName = 'clt_letter_users';

    /**
     * 更新余额
     *
     * 详细说明
     * @形参
     * @访问      公有
     * @返回值    string
     * @throws
     * smy
     */
    public static function updateMoney($user_id, $money) {
        //更新报错
        if ($money > 0) {
            $sql = 'UPDATE ' . self::$tableName . ' SET money = money + ' . $money . ' WHERE user_id = ' . $user_id;
        } else {
            $sql = 'UPDATE ' . self::$tableName . ' SET money =money  ' . $money . ' WHERE user_id = ' . $user_id . ' and money ' . $money . '>=0';
        }
        $return = parent::execute($sql);
        if ($return > 0) {
            return true;
        }
        return false;
    }
    
    
    /**
     * 更新佣金
     *
     * 详细说明
     * @形参
     * @访问      公有
     * @返回值    string
     * @throws
     * smy
     */
    public static function updateWithdrawMoney($user_id, $money) {
        //更新报错
        if ($money > 0) {
            $sql = 'UPDATE ' . self::$tableName . ' SET withdraw_money = withdraw_money + ' . $money . ' WHERE user_id = ' . $user_id;
        } else {
            $sql = 'UPDATE ' . self::$tableName . ' SET withdraw_money =withdraw_money  ' . $money . ' WHERE user_id = ' . $user_id . ' and withdraw_money ' . $money . '>=0';
        }
        $return = parent::execute($sql);
        if ($return > 0) {
            return true;
        }
        return false;
    }
    

    /**
     * 更新会员积分字段
     *
     * 详细说明
     * @形参
     * @访问      公有
     * @返回值    string
     * @throws
     * smy
     */
    function UpdateUserIntegral($user_id, $integral, $add_type = '+') {
        //更新报错
        if ($add_type == '+') {
            $sql = 'UPDATE ' . $this->_tableName . ' SET point = point ' . $add_type . $integral . ' WHERE user_id = ' . $user_id;
        } elseif ($add_type == '-') {
            $sql = 'UPDATE ' . $this->_tableName . ' SET point = IF(point >= ' . $integral . ',point ' . $add_type . $integral . ',point ' . $add_type . ' 0) WHERE user_id = ' . $user_id;
        } else {
            return FALSE;
        }
        $return = parent::execute($sql);
        if ($return > 0) {
            return true;
        }
        return false;
    }

    /**
     * 更新会员等级
     *
     * 详细说明
     * @形参
     * @访问      公有
     * @返回值    string
     * @throws
     * smy
     */
    function UpdateUserLv($user_id, $integral) {
        $sql = 'UPDATE ' . $this->_tableName . ' SET lv = lv +' . $integral . ' WHERE user_id = ' . $user_id;
        $return = parent::execute($sql);
        if ($return > 0) {
            return true;
        }
        return false;
    }

}
